$(document).ready(function(){
	(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
		  var cacheImage = document.createElement('img');
		  cacheImage.src = arguments[i];
		  cache.push(cacheImage);
		}
	  }
	})(jQuery)

	jQuery.preLoadImages("img/groups_new/group1_hover.png", "img/groups_new/group2_hover.png", "img/groups_new/group3_hover.png", "img/groups_new/group4_hover.png", "img/groups_new/group5_hover.png", "img/groups_new/group6_hover.png", "img/groups_new/group7_hover.png", "img/groups_new/group8_hover.png", "img/groups_new/group9_hover.png", "img/groups_new/group10_hover.png", "img/groups_new/group11_hover.png", "img/groups_new/group12_hover.png", "img/groups_new/group13_hover.png");


	$(".icon").hover(function(event){
		if($(this).children("img").length != 0)
		{
		  var hoverImg = HoverImgOf(NormalImgOf($(this).children("img").attr("src")));
		  $(this).not('.selected').children("img").attr("src", hoverImg);
		}
	},function(){
		if($(this).children("img").length != 0)
		{
		  var normalImg = NormalImgOf($(this).children("img").attr("src"));
		  $(this).not('.selected').children("img").attr("src", normalImg);
		}
   });

	$(".icon").click(function(event){

		if($(this).hasClass("selected"))
		   return;

		window.location.hash = $(this).attr("id").replace('p_','');
		$("#stage").html("").html($(this).children("div.content").html());
		if($(this).attr("id") == "p_info")
		{
			$("#stage").addClass('landing');
			$("#stage_bottom").addClass('landing');
		}
		else
		{
			$("#stage").removeClass('landing');
			$("#stage_bottom").removeClass('landing');
		}

		// deselect all other icons
		$("div.selected").each(function(index) {
			if($(this).children("img").length != 0)
			{
			var normalImg = NormalImgOf($(this).children("img").attr("src"));
			$(this).children("img").attr("src", normalImg);
			}
		  });
		$(".icon").removeClass("selected");
		$(this).addClass("selected");
		
		if($(this).children("img").length != 0)
		{
		var hoverImg = HoverImgOf(NormalImgOf($(this).children("img").attr("src")));
		$(this).children("img").attr("src", hoverImg);
		}
		updateFancybox();
		$('a.email').nospam({
							  replaceText: true,
							  filterLevel: 'low'
							});
	});

	readHash();
	updateFancybox();

});
function readHash()
{
	if(window.location.hash != "")
		$('div#p_'+window.location.hash.replace('#','')).trigger('click');
	else
		$('#p_info').trigger('click');
}
function updateFancybox()
{
	$('a.fancybox').fancybox({
				'titleShow'		: false,
				'transitionIn'	: 'fade',
				'transitionOut'	: 'fade'
			});
	

}

function HoverImgOf(filename)
{

   var re = new RegExp("(.+)\\.(gif|png|jpg)", "g");
   return filename.replace(re, "$1_hover.$2");
}
function NormalImgOf(filename)
{
   var re = new RegExp("(.+)_hover\\.(gif|png|jpg)", "g");
   return filename.replace(re, "$1.$2");

}